﻿/*
This file (JS library) was written by CanhNV1 (Team Leader)
*/

function CheckBrowserVersions (errorUrls)
{
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName  = navigator.appName;
    var fullVersion  = ''+parseFloat(navigator.appVersion); 
    var majorVersion = parseInt(navigator.appVersion,10);
    var nameOffset,verOffset,ix;
    
    if ((verOffset=nAgt.indexOf("MSIE"))!=-1) 
    {
        browserName = "IE"; // Microsoft Internet Explorer
        fullVersion = nAgt.substring(verOffset+5);
    }
    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) 
    {
         browserName = "Firefox";
         fullVersion = nAgt.substring(verOffset+8);
         
    }
    else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) // Also apply to browser Comodo dragon
    {
         browserName = "Chrome";
         
         if (window.navigator.appVersion.match(/Chrome\/(\d+)\./) != null || 
            window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1] != null)
         {
            fullVersion = window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1];
         }
    }
    else
    {
        // Opera, Safari
        window.location.href = errorUrls;
    }
    
    majorVersion = parseInt(''+fullVersion,10);
    if (isNaN(majorVersion)) 
    {
         fullVersion  = ''+parseFloat(navigator.appVersion); 
         majorVersion = parseInt(navigator.appVersion,10);
    }

    if 
    (
        (browserName == "Firefox" && majorVersion < 3 )
        ||
        (browserName == "IE" && majorVersion < 7 )
        || 
        (browserName == "Chrome" && majorVersion < 6 )
    )
    {
        window.location.href = errorUrls;
    }
    
    //Full version as string:
    //window.navigator.appVersion.match(/Chrome\/(.*?) /)[1];

    //First digit only as int:
    //var ver = parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10);
}

// Trim a string. Remove all blank spaces at the beginning and at the end of string.
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

// Set focus on input text 
function SetFocus(objFieldArg)
{
    var objField = document.getElementById(objFieldArg);
    
    if (objField.disabled == false)
    {
        objField.focus();
    }
}

function CheckSession(objSession, urlToCome)
{
    if (objSession == null || objSession.toString().Trim().length <= 0)
    {
        window.location = urlToCome;
    }
}

// Confirm to delete
function ForceDelete()
{
    return confirm("Are you sure to delete the item?");
}

// Validate email with regular expression
function ValidEmail(argEmailField, argMessField, message)
{
    var emailField = document.getElementById(argEmailField);
    var messField = document.getElementById(argMessField);
    
    var emailString = emailField.value;
    var pattern=new RegExp("/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/");
    
    if(pattern.test(emailString))
    {
        messField.innerHTML = "";
        return true; // The email is valid
    }
    else
    {   
        messField.innerHTML = message;
        emailField.focus();
        return false; // The email is invalid
    }
}


// Check string is null or empty?. Should apply to text fields only.
function IsEmptyTextField(argTextField, argMessField, message)
{
    var textField = document.getElementById(argTextField);
    var messField = document.getElementById(argMessField);
    
    var values = new String(textField.value);
    if (values.Trim().length<=0)
    {
        if (textField.disabled == false)
        {
            textField.focus();
        }
        messField.innerHTML = message;
        return true; // The textField is empty then focus at it
    }
    else
    {
        messField.innerHTML = "";
        return false; // The textField is not empty
    }
}


// Check values of two fields are same
function TwoFieldSameValue(textFieldFirstArg, textFieldSecondArg, messFieldArg, message)
{
    var textFieldFirst = document.getElementById(textFieldFirstArg);
    var textFieldSecond = document.getElementById(textFieldSecondArg);
    var messField = document.getElementById(messFieldArg);
    
    var valuesFirst = new String(textFieldFirst.value);
    var valuesSecond = new String(textFieldSecond.value);
    
    if (valuesFirst.Trim() == valuesSecond.Trim())
    {
        messField.innerHTML = "";
        return true; // The values of two fields are same
    }
    else
    {
        messField.innerHTML = message;
        if (textFieldSecond.disabled == false)
        {
            textFieldSecond.focus();
        }
        return false; // The values of two fields are not same
    }
}

// Force fixed length of string
function IsValidStringLength(textFieldArg, length, messFieldArg, message)
{
    var textField = document.getElementById(textFieldArg);
    var messField = document.getElementById(messFieldArg);
    
    var values = new String(textField.value);
    
    if (values.Trim().length < length)
    {   
        if (textField.disabled == false)
        {
           textField.focus();   
        }
        messField.innerHTML = message;
        return false;
    }
    else
    {
        messField.innerHTML = "";
        return true;
    }
}


// Not a blank space to allowed
function IsBlankSpaceInString(textFieldArg, messFieldArg, message)
{
    var textField = document.getElementById(textFieldArg);
    var messField = document.getElementById(messFieldArg);
    
    var values = new String(textField.value);
    
    if (values.Trim().indexOf(" ") >= 0)
    {
         messField.innerHTML = message;
         return true;
    }
    else
    {
        messField.innerHTML = "";
        return false;
    }
}

// Show message if the account to delete is the same as the active account
function WarningDelete()
{
    alert ("You can not delete the account. Because it is in use.");
}

// Check whether a value is numeric
function IsNumeric(input)
{
    return !isNaN(input);
}

// Allow only numbers to input
function OnlyNumbers(evt)
{
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
}
